package cn.xlbweb.cli.controller;

import cn.xlbweb.cli.common.ServerResponse;
import cn.xlbweb.cli.common.TableResponse;
import cn.xlbweb.cli.common.TreeResponse;
import cn.xlbweb.cli.model.Role;
import cn.xlbweb.cli.pojo.dto.RolesListDTO;
import cn.xlbweb.cli.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: bobi
 * @date: 2019-02-04 00:55
 * @description:
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @GetMapping("listRoles")
    @RequiresPermissions("role:listRoles")
    public TableResponse listRoles(RolesListDTO rolesListDTO,
                                   @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return iRoleService.listRoles(rolesListDTO, pageNum, pageSize);
    }

    @PostMapping("insertRole")
    @RequiresPermissions("role:insertRole")
    public ServerResponse insertRole(Role role) {
        return iRoleService.insertRole(role);
    }

    @PutMapping("updateRole")
    @RequiresPermissions("role:updateRole")
    public ServerResponse updateRole(Role role) {
        return iRoleService.updateRole(role);
    }

    @DeleteMapping("deleteRole")
    @RequiresPermissions("role:deleteRole")
    public ServerResponse deleteRole(String ids) {
        return iRoleService.deleteRole(ids);
    }

    @GetMapping("getTree")
    @RequiresPermissions("role:getTree")
    public TreeResponse getTree(Integer roleId) {
        return iRoleService.getTree(roleId);
    }

    @PostMapping("authorize")
    @RequiresPermissions("role:authorize")
    public ServerResponse authorize(Integer roleId, String permissionIds) {
        return iRoleService.authorize(roleId, permissionIds);
    }
}
